import { getCategory1IdApi, getCategory2IdApi, getCategory3IdApi } from '@/api/product/getCategoryApi'
const state = {
  category: {
    category1Id: undefined,
    category2Id: undefined,
    category3Id: undefined,
    category1List: [],
    category2List: [],
    category3List: []
  }
}
const mutations = {
  CATEGORY1LIST(state, data) {
    state.category.category1List = data
  },
  CATEGORY2LIST(state, data) {
    state.category.category2Id = undefined
    state.category.category3Id = undefined
    state.category.category2List = []
    state.category.category3List = []
    state.category.category2List = data
  },
  CATEGORY3LIST(state, data) {
    state.category.category3Id = undefined
    state.category.category3List = data
  },
  RESETSTORE(state) {
    state.category.category1Id = undefined
    state.category.category2Id = undefined
    state.category.category3Id = undefined
    state.category.category1List = []
    state.category.category2List = []
    state.category.category3List = []
  }
}
const actions = {
  async getCategory1Id({ commit }) {
    const res = await getCategory1IdApi()
    commit('CATEGORY1LIST', res.data)
  },
  async getCategory2Id({ commit, state }) {
    const res = await getCategory2IdApi(state.category.category1Id)
    commit('CATEGORY2LIST', res.data)
  },
  async getCategory3Id({ commit, state }) {
    const res = await getCategory3IdApi(state.category.category2Id)
    commit('CATEGORY3LIST', res.data)
  },
  resetStore({ commit }) {
    commit('RESETSTORE')
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
